//
//  XMTitleAlterView.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/5/5.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "KBaseAlterView.h"

@interface KBaseAlterView ()

@property (nonatomic,strong) UIView * showView;

@end

@implementation KBaseAlterView

-(instancetype)init{
    
    if (self = [super init]) {
        [self setUI];
    }
    return self;
}

-(void)setUI{
    
    self.backgroundColor = [UIColor clearColor];
    
    //背景control
    [self.coverControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    
    //内容
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@300);
        make.height.equalTo(@(180));
    }];
}


#pragma mark 动画

- (void)show{
    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [self showWithView:keyWindow];

}

//在view上显示
-(void)showWithView:(UIView *)view{
    if (self.showView == nil) {
        [view addSubview:self];
        self.showView = view;
    }
    
    self.isShow = YES;
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(view);
    }];
    
    self.contentView.alpha = 0;
    self.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    MJWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        weakSelf.contentView.alpha = 1;
    }];
}

//隐藏不移除
-(void)existHide{
    self.isShow = NO;

    MJWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        
        weakSelf.contentView.alpha = 0;
        weakSelf.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        
    } completion:^(BOOL finished) {

    }];
}

//隐藏+移除
- (void)hide
{
    self.isShow = NO;

    MJWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        
        weakSelf.contentView.alpha = 0;
        weakSelf.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        
    } completion:^(BOOL finished) {
        [self.coverControl removeAllSubviews];
        [self.coverControl removeFromSuperview];
        [self.contentView removeAllSubviews];
        [self.contentView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

#pragma mark ----getting&setting----

//背景颜色control
-(UIControl *)coverControl{
    if (!_coverControl) {
        _coverControl = [[UIControl alloc]init];
        _coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        [_coverControl addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_coverControl];
        
    }
    return _coverControl;
}

//内容
-(UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = DMWhiteColor;
        [self addSubview:_contentView];
    }
    return _contentView;
    
}


@end
